/*
 * Copyright 2009 Tran Nhut Trung.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.gdevelop.gwtxp.rebind.gwt;


import com.gdevelop.gwtxp.rebind.AbstractTag;
import com.gdevelop.gwtxp.rebind.Tag;

import com.google.gwt.core.ext.typeinfo.NotFoundException;

import org.w3c.dom.Attr;

public abstract class HTMLTableTag extends AbstractTag{
  public HTMLTableTag() {
  }
  
  public void generateInitStart() throws NotFoundException {
    String vars = this.getAttribute("vars");
    if ((vars != null) && (vars.length() > 0)){
      out.println(vars);
    }
    
    super.generateInitStart();
  }
  
  protected void generateSetAttributeByExpression(Attr attr) throws NotFoundException {
    if ("vars".equals(attr.getLocalName())){
      return;
    }
    super.generateSetAttributeByExpression(attr);
  }

  public void generateAddTableCell(TableCellTag cellTag, Tag child) throws NotFoundException{
    String row = getRow(cellTag);
    String col = cellTag.getAttribute("col");
    if ((row == null) || (col == null)){
      throw new RuntimeException("TableCell must have row and col attribute.");
    }
    
    out.println(this.varName + ".setWidget(" + row + ", " + col + ", " + child.getVarName() + ");");
  }
  
  public void generateSetCellAttibutes(TableCellTag cellTag, Tag child){
    String row = getRow(cellTag);
    String col = cellTag.getAttribute("col");
    if ((row == null) || (col == null)){
      throw new RuntimeException("TableCell must have row and col attribute.");
    }

    String height = cellTag.getAttribute("height");
    if (height != null){
      height = "\"" + height + "\"";
      setCellFormatter(row, col, "height", height);
    }
    String width = cellTag.getAttribute("width");
    if (width != null){
      width = "\"" + width + "\"";
      setCellFormatter(row, col, "width", width);
    }

    String horizontalAlignment = cellTag.getAttribute("horizontalAlignment");
    if (horizontalAlignment != null){
      horizontalAlignment = horizontalAlignment.toLowerCase();
      if ("left".equals(horizontalAlignment)){
        horizontalAlignment = "HasHorizontalAlignment.ALIGN_LEFT";
      }else if ("center".equals(horizontalAlignment)){
        horizontalAlignment = "HasHorizontalAlignment.ALIGN_CENTER";
      }else if ("right".equals(horizontalAlignment)){
        horizontalAlignment = "HasHorizontalAlignment.ALIGN_RIGHT";
      }else{
        horizontalAlignment = null;
      }
      if (horizontalAlignment != null){
        setCellFormatter(row, col, "horizontalAlignment", horizontalAlignment);
      }
    }

    String verticalAlignment = cellTag.getAttribute("verticalAlignment");
    if (verticalAlignment != null){
      verticalAlignment = verticalAlignment.toLowerCase();
      if ("top".equals(verticalAlignment)){
        verticalAlignment = "HasVerticalAlignment.ALIGN_TOP";
      }else if ("middle".equals(verticalAlignment)){
        verticalAlignment = "HasVerticalAlignment.ALIGN_MIDDLE";
      }else if ("bottom".equals(verticalAlignment)){
        verticalAlignment = "HasVerticalAlignment.ALIGN_BOTTOM";
      }else{
        verticalAlignment = null;
      }
      if (verticalAlignment != null){
        setCellFormatter(row, col, "verticalAlignment", verticalAlignment);
      }
    }

    String styleName = cellTag.getAttribute("styleName");
    if (styleName != null){
      styleName = "\"" + styleName + "\"";
      setCellFormatter(row, col, "styleName", styleName);
    }
    String stylePrimaryName = cellTag.getAttribute("stylePrimaryName");
    if (stylePrimaryName != null){
      stylePrimaryName = "\"" + stylePrimaryName + "\"";
      setCellFormatter(row, col, "stylePrimaryName", stylePrimaryName);
    }

    String visible = cellTag.getAttribute("visible");
    if (visible != null){
      if (!"true".equals(visible)){
        visible = "false";
      }
      setCellFormatter(row, col, "visible", visible);
    }

    String wordWrap = cellTag.getAttribute("wordWrap");
    if (wordWrap != null){
      if (!"true".equals(wordWrap)){
        wordWrap = "false";
      }
      setCellFormatter(row, col, "wordWrap", wordWrap);
    }
  }
  
  protected String getRow(TableCellTag cellTag){
    return cellTag.getAttribute("row");
  }

  protected void setCellFormatter(String row, String col, String propName, String value){
    if (value != null){
      out.println(this.getVarName() + ".getCellFormatter()." + elContext.getSetMethodName(propName) + "(" + row + ", " + col + ", " + value + ");");
    }
  }
}
